package com.v9.system.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.v9.common.dto.IdDto;
import com.v9.common.dto.PageInfoDto;
import com.v9.common.utils.StringUtils;
import com.v9.common.web.AbstractController;
import com.v9.system.domain.CodeTableType;
import com.v9.system.service.CodeTableTypeService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.Data;
import org.hibernate.validator.constraints.Length;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import javax.validation.constraints.NotEmpty;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author lunshi
 */
@RestController
@Tag(name = "码表类型")
@RequestMapping(value = "/system/dict/type", method = RequestMethod.POST)
public class CodeTableTypeController extends AbstractController {
    @Resource
    private CodeTableTypeService codeTableTypeService;

    @RequestMapping(value = "save")
    @Operation(summary = "保存")
    public void save(@RequestBody @Valid CodeTableTypeDto dto) {
        CodeTableType t = clone(CodeTableType.class,dto);
        t.setCode(dto.getCode());
        codeTableTypeService.saveOrInsert(t);
    }

    @RequestMapping("remove")
    @Operation(summary = "删除")
    public void remove(@RequestBody List<IdDto> dtos) {
        List<Long> ids = dtos.stream().map(IdDto::getId).collect(Collectors.toList());
        codeTableTypeService.remove(false,ids.toArray(new Long[ids.size()]));
    }

    @RequestMapping("view")
    @Operation(summary = "查看")
    public CodeTableTypeDto view(@RequestBody IdDto dto) {
        return clone(CodeTableTypeDto.class, codeTableTypeService.findById(dto.getId()));
    }

    @RequestMapping("list")
    @Operation(summary = "list")
    public Page<CodeTableTypeDto> list(@RequestBody CodeTableQueryDto dto) {
        QueryWrapper<CodeTableType> queryWrapper = new QueryWrapper<>();
        if(StringUtils.isNotBlank(dto.getName())) {
            queryWrapper.like("name", dto.getName());
        }
        Page<CodeTableType> sourcepage = codeTableTypeService.queryByPage(queryWrapper, dto.getPageable());
        return clone(CodeTableTypeDto.class, sourcepage);
    }

    @Data
    public static class CodeTableQueryDto extends PageInfoDto {
        @NotEmpty(message = "非法操作，codeType不能为空")
        private String name;
    }

    @Data
    public static final class CodeTableTypeDto {
        private Long id;
        /**
         * 名称
         */
        @NotEmpty(message = "名称不能为空")
        @Length(max = 32)
        private String name;
        /**
         * 备注
         */
        @Length(max = 64)
        private String remark;
        /**
         * 编码
         */
        @NotEmpty(message = "编码不能为空")
        @Length(max = 32)
        private String code;
    }
}
